<template>
  <aside class="sidebar">
    <!-- 企业信息区域 -->
    <div class="company-info">
      <div class="company-header">
        <div class="company-logo">
          <div class="logo-square">{{ getCompanyInitial() }}</div>
        </div>
        <div class="company-title">
          <h2 class="space-title">{{ $t('enterpriseHome.enterpriseSpace') }}</h2>
        </div>
      </div>
      <div class="company-details">
        <h3 class="company-name">{{ companyInfo.companyName || '加载中...' }}</h3>
        <div class="company-id">
          <span>{{ companyInfo.socialCreditCode || '加载中...' }}</span>
          <div class="id-controls">
            <el-icon size="12"><ArrowUp /></el-icon>
            <el-icon size="12"><ArrowDown /></el-icon>
          </div>
        </div>
      </div>
    </div>

    <!-- 导航菜单 -->
    <nav class="navigation-menu">
      <a href="#" class="nav-item" :class="{ active: currentRoute === 'enterprise-home' }" @click.prevent="goToHome">
        <el-icon size="18"><House /></el-icon>
        <span>{{ $t('enterpriseHome.home') }}</span>
      </a>
      <a href="#" class="nav-item" :class="{ active: currentRoute === 'enterprise-space' }" @click.prevent="goToCreateDemand">
        <el-icon size="18"><Document /></el-icon>
        <span>{{ $t('enterpriseHome.createDemand') }}</span>
      </a>
      <a href="#" class="nav-item" :class="{ active: currentRoute === 'enterprise-application-records' }" @click.prevent="goToApplicationRecords">
        <el-icon size="18"><List /></el-icon>
        <span>{{ $t('enterpriseHome.applicationRecords') }}</span>
      </a>
      <a href="#" class="nav-item" :class="{ active: currentRoute === 'enterprise-certification' }" @click.prevent="goToCertification">
        <el-icon size="18"><Check /></el-icon>
        <span>{{ $t('enterpriseHome.enterpriseCertification') }}</span>
      </a>
      <a href="#" class="nav-item" :class="{ active: currentRoute === 'enterprise-messages' }" @click.prevent="goToMessages">
        <el-icon size="18"><Message /></el-icon>
        <span>{{ $t('enterpriseHome.messageCenter') }}</span>
      </a>
      <a href="#" class="nav-item" :class="{ active: currentRoute === 'enterprise-settings' }" @click.prevent="goToSettings">
        <el-icon size="18"><Setting /></el-icon>
        <span>{{ $t('enterpriseHome.accountSettings') }}</span>
      </a>
    </nav>
  </aside>
</template>

<script setup>
import { computed, ref, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useI18n } from 'vue-i18n'
import { 
  ArrowUp, ArrowDown, House, Document, List, Message, Setting, Check
} from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'

const router = useRouter()
const route = useRoute()
const { t } = useI18n()

// 企业信息状态
const companyInfo = ref({
  companyName: '',
  socialCreditCode: '',
  email: ''
})

// 当前路由名称
const currentRoute = computed(() => route.name)

// 获取企业信息
const fetchEnterpriseProfile = async () => {
  try {
    const token = localStorage.getItem('access_token')
    if (!token) {
      console.warn('没有有效的认证token')
      return
    }
    
    const response = await fetch('/api/api/v1/enterprise/profile/complete', {
      method: 'GET',
      headers: {
        'Authorization': `Bearer ${token}`,
        'Content-Type': 'application/json'
      }
    })
    
    if (response.ok) {
      const result = await response.json()
      
      if (result.code === 'SUCCESS' && result.data && result.data.companyInfo) {
        const data = result.data.companyInfo
        companyInfo.value = {
          companyName: data.companyFullName || data.companyName || '企业名称',
          socialCreditCode: data.socialCreditCode || data.creditCode || '',
          email: data.contactEmail || data.email || ''
        }
      }
    } else if (response.status === 404) {
      console.log('企业信息API不存在，使用默认信息')
      // 如果API不存在，使用默认信息
      companyInfo.value = {
        companyName: '企业名称',
        socialCreditCode: '',
        email: ''
      }
    } else {
      console.warn('获取企业信息失败:', response.status)
    }
  } catch (error) {
    console.warn('获取企业信息时发生错误，使用默认信息:', error.message)
    // 出错时使用默认信息
    companyInfo.value = {
      companyName: '企业名称',
      socialCreditCode: '',
      email: ''
    }
  }
}

// 获取公司名称首字母
const getCompanyInitial = () => {
  const name = companyInfo.value.companyName
  if (!name) return 'G'
  
  // 如果是中文，取第一个字符
  if (/[\u4e00-\u9fa5]/.test(name)) {
    return name.charAt(0)
  }
  
  // 如果是英文，取第一个字母
  return name.charAt(0).toUpperCase()
}

// 导航方法
const goToHome = () => {
  router.push({ name: 'enterprise-home' })
}

const goToCreateDemand = () => {
  router.push({ name: 'enterprise-space' })
}

const goToApplicationRecords = () => {
  router.push({ name: 'enterprise-application-records' })
}

const goToCertification = () => {
  router.push({ name: 'enterprise-certification' })
}

const goToMessages = () => {
  router.push({ name: 'enterprise-messages' })
}

const goToSettings = () => {
  router.push({ name: 'enterprise-settings' })
}

// 组件挂载时获取企业信息
onMounted(() => {
  fetchEnterpriseProfile()
})
</script>

<style scoped>
/* 左侧导航栏 */
.sidebar {
  width: 280px;
  background-color: #f8f9fa;
  border-right: 1px solid #e9ecef;
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  /* 添加阴影效果，避免白色边缘 */
  box-shadow: 2px 0 8px rgba(0, 0, 0, 0.1);
  z-index: 10;
  /* 添加平滑过渡效果 */
  transition: all 0.3s ease;
}

.company-info {
  padding: 20px;
  border-bottom: 1px solid #e9ecef;
  background-color: #ffffff;
  /* 添加圆角效果 */
  border-radius: 0 0 8px 0;
}

.company-header {
  display: flex;
  align-items: center;
  margin-bottom: 15px;
}

.company-logo {
  margin-right: 15px;
}

.logo-square {
  width: 40px;
  height: 40px;
  background-color: #1976d2;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
  font-size: 18px;
  /* 添加圆角效果 */
  border-radius: 6px;
  /* 添加阴影效果 */
  box-shadow: 0 2px 8px rgba(25, 118, 210, 0.3);
  transition: all 0.3s ease;
}

.logo-square:hover {
  transform: scale(1.05);
  box-shadow: 0 4px 12px rgba(25, 118, 210, 0.4);
}

.company-title {
  flex: 1;
}

.space-title {
  font-size: 18px;
  font-weight: 600;
  color: #333;
  margin: 0;
  line-height: 1.4;
  /* 添加文字阴影 */
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

.company-name {
  font-size: 14px;
  font-weight: 600;
  color: #333;
  margin: 0 0 8px 0;
  line-height: 1.4;
}

.company-id {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 12px;
  color: #666;
}

.id-controls {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.navigation-menu {
  flex: 1;
  padding: 20px 0;
  background-color: #f8f9fa;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 20px;
  color: #666;
  text-decoration: none;
  font-size: 14px;
  transition: all 0.3s ease;
  border-left: 3px solid transparent;
  /* 添加hover时的背景色变化 */
  background-color: transparent;
  /* 添加圆角效果 */
  border-radius: 0 6px 6px 0;
  margin: 2px 10px 2px 0;
}

.nav-item:hover {
  background-color: #e3f2fd;
  color: #1976d2;
  transform: translateX(4px);
  box-shadow: 0 2px 8px rgba(25, 118, 210, 0.15);
}

.nav-item.active {
  background-color: #e3f2fd;
  color: #1976d2;
  border-left-color: #1976d2;
  font-weight: 500;
  box-shadow: 0 2px 8px rgba(25, 118, 210, 0.2);
}

.nav-item span {
  flex: 1;
  /* 添加文字过渡效果 */
  transition: all 0.3s ease;
}

/* 添加图标过渡效果 */
.nav-item .el-icon {
  transition: all 0.3s ease;
}

.nav-item:hover .el-icon {
  transform: scale(1.1);
  color: #1976d2;
}

/* 添加页面加载时的平滑效果 */
.sidebar {
  animation: slideInLeft 0.4s ease-out;
}

@keyframes slideInLeft {
  from {
    opacity: 0;
    transform: translateX(-20px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}
</style>